from django.contrib.auth import get_user_model
from django.db import models

from tag.models import Tag

User = get_user_model()


class QuestionPicture(models.Model):
    image = models.CharField(max_length=300, blank=True, null=True)


class Question(models.Model):
    title = models.CharField(max_length=300, blank=True)
    content = models.TextField(blank=True)
    tags = models.ManyToManyField(Tag)
    pictures = models.ManyToManyField(QuestionPicture)
    creator = models.ForeignKey(User, related_name='questions', on_delete=models.CASCADE)
    created_time = models.DateTimeField(auto_now_add=True)
    praise = models.IntegerField(default=0)

    def __str__(self):
        return self.title


class Banner(models.Model):
    """轮播热门问题"""
    related_id = models.IntegerField(default=1)
    image = models.URLField(max_length=200)
    type = models.CharField(max_length=300, default='question')


class QuestionPraiseRelationship(models.Model):
    question = models.ForeignKey(Question, related_name='qpr', on_delete=models.CASCADE)
    user = models.ForeignKey(User, related_name='qpr', on_delete=models.CASCADE)

    praise = models.BooleanField(default=False)

    created_time = models.DateTimeField(auto_now_add=True)


class QuestionFocus(models.Model):
    """关注收藏"""
    question = models.ForeignKey(Question, related_name='questionfocus', on_delete=models.CASCADE)
    creator = models.ForeignKey(User, related_name='questionfocus', on_delete=models.CASCADE)
    created_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = (("creator", "question"),)


class QuestionCollection(models.Model):
    """收藏"""
    question = models.ForeignKey(Question, related_name='questioncollection', on_delete=models.CASCADE)
    creator = models.ForeignKey(User, related_name='questioncollection', on_delete=models.CASCADE)
    created_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = (("creator", "question"),)
